package com.ksc.storekeeper.server.web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ksc.storekeeper.server.utils.AuthenticationUtil;

@Controller
public class LoginController {

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(ModelMap modelMap, @RequestParam(value = "errormessage", required = false) String errormessage,
			@RequestParam(value = "spring-security-redirect", required = false) String redirect) {
		if (AuthenticationUtil.getCurrentUser() != null) {
			return "redirect:/main";
		}
		
		modelMap.put("action", "j_spring_security_check");
		modelMap.put("errormessage", errormessage == null ? "" : errormessage);
		modelMap.put("msg","Login is required.");
		return "login";
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String getHome() {
		if (AuthenticationUtil.getCurrentUser() != null) {
			return "redirect:/main";
		}
		return "redirect:/login";
	}
}
